package com.example.springpethospital.controller;

import com.example.springpethospital.entity.AdminMethod;
import com.example.springpethospital.entity.UserType;
import com.example.springpethospital.service.UserService;
import com.example.springpethospital.util.TokenUtil;
import com.example.springpethospital.util.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true", allowedHeaders = "*")
public class LoginController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Object login(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Integer userid = userService.LoginAndGetID(username, password);
        if (userid == null) {
            return Utility.newResponseObject("用户名或密码不正确");
        } else {
            Map<String, Object> dat = new HashMap<>();
            String token = TokenUtil.sign(username, userid);
            int usertype = userService.getUserType(userid);
            dat.put("token", token);
            dat.put("usertype", usertype);
            return Utility.newResponseObject(true, dat);
        }
    }

    @PostMapping("/register")
    public Object register(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserType userType = UserType.User;  //新注册统一为用户
        try {
            userService.Register(username, password, userType);
            return Utility.newResponseObject(true);
        } catch (IllegalArgumentException ex) {
            return Utility.newResponseObject(ex.getMessage());
        }
    }

    @PostMapping("/userManage/changePassword")
    public Object changePassword(HttpServletRequest request) {
        String token = request.getParameter("token");
        String oldPassword = request.getParameter("oldPassword");
        String newPassword = request.getParameter("newPassword");
        try {
            userService.ChangePassword(token, oldPassword, newPassword);
            return Utility.newResponseObject(true);
        } catch (RuntimeException ex) {
            return Utility.newResponseObject(ex.getMessage());
        }
    }

    @PostMapping("/userManage/changeUsername")
    public Object changeUsername(HttpServletRequest request) {
        String token = request.getParameter("token");
        String newUsername = request.getParameter("newUsername");
        try {
            userService.ChangeUsername(token, newUsername);
            return Utility.newResponseObject(true);
        } catch (RuntimeException ex) {
            return Utility.newResponseObject(ex.getMessage());
        }
    }

    @AdminMethod
    @PostMapping("/userManage/setUserType")
    public Object setUserType(HttpServletRequest request) {
        String sTargetID = request.getParameter("id");
        String sUserType = request.getParameter("usertype");
        try {
            int iUserType = Integer.parseInt(sUserType);
            int iTargetID = Integer.parseInt(sTargetID);
            if (userService.setUserType(iTargetID, UserType.toUserType(iUserType)))
                return Utility.newResponseObject(true);
            else
                return Utility.newResponseObject("未找到相应用户");
        } catch (NumberFormatException ex) {
            return Utility.newResponseObject("ID或用户类型格式不正确");
        }
    }

    @AdminMethod
    @RequestMapping("/userManage/searchUser")
    public Object searchUser(HttpServletRequest request, @RequestParam("username") String username) {
        String sPaging = request.getParameter("paging");
        try {
            if (sPaging != null) {
                int paging = Integer.parseInt(sPaging);
                return Utility.newResponseObject(true, userService.getTotalPages(username), userService.searchUser(username, paging));
            } else {
                return Utility.newResponseObject(true, userService.searchUser(username, null));
            }
        } catch (NumberFormatException ex) {
            return Utility.newResponseObject("分页格式不正确，请输入0-32767的整数");
        }
    }

    @AdminMethod
    @PostMapping("/userManage/removeUser")
    public Object removeUser(HttpServletRequest request, @RequestParam("id") Integer userid) {
        if (userService.removeUser(userid))
            return Utility.newResponseObject(true);
        else
            return Utility.newResponseObject("未找到相应用户");
    }
}
